package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.qms.service.FlightOrderService;
import cn.buk.tms.exception.BaseException;
import cn.buk.tms.flight.dto.FlightExternalOrderDto;
import cn.buk.tms.flight.service.AutoTicketService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/order/flight")
public class OrderFlightController extends BaseController {

  private static final Logger logger = LogManager.getLogger(OrderFlightController.class);

  @Autowired
  public OrderFlightController(FlightOrderService flightOrderService,
                         AutoTicketService autoTicketService) {
    super();
    this.flightOrderService = flightOrderService;
    this.autoTicketService = autoTicketService;
  }


  private final FlightOrderService flightOrderService;

  private final AutoTicketService autoTicketService;




  /**
   * 向外部系统提交订单
   * @param request 请求
   * @param orderId 订单id
   * @param supplierId 供应商id
   * @return 结果
   */
  @PostMapping(value = "/{orderId}/externalOrder")
  public JsonResult createExternalFlightOrder(HttpServletRequest request,
                                              @PathVariable("orderId") int orderId,
                                              @RequestParam(value = "supplierId", defaultValue = "0") int supplierId
  ) throws BaseException {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return flightOrderService.createExternalFlightOrder(enterpriseId, username, orderId, supplierId);
  }

  /**
   * 查询外部系统订单
   * @param request 请求
   * @param orderId 订单id
   * @return 结果
   */
  @GetMapping(value = "/{orderId}/externalOrders")
  public List<FlightExternalOrderDto> searchExternalFlightOrders(HttpServletRequest request,
                                                                 @PathVariable("orderId") int orderId) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return flightOrderService.searchExternalFlightOrders(enterpriseId, username, orderId);
  }

  /**
   * 向外部系统确认订单可以开票
   * @param request 请求
   * @param externalOrderId 外部订单id
   * @return 结果
   */
  @PostMapping(value = "/external/{orderId}/confirm")
  public JsonResult confirmExternalFlightOrder(HttpServletRequest request,
                                               @PathVariable("orderId") int externalOrderId
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return flightOrderService.confirmExternalFlightOrder(enterpriseId, username, externalOrderId);
  }

  /**
   * 同步外部订单状态
   */
  @PostMapping(value = "/external/{orderId}")
  public JsonResult syncFlightOrderStatus(HttpServletRequest request,
                                          @PathVariable("orderId") int externalOrderId
  ) throws BaseException {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return flightOrderService.syncExternalOrderStatus(enterpriseId, username, externalOrderId);
  }
}
